package com.lagou.ex.controller;

import com.lagou.ex.domain.LgResult;
import com.lagou.ex.entity.TbUser;
import com.lagou.ex.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/user")
public class LoginController {
    @Autowired
    private UserService userService;

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public LgResult login(@RequestBody TbUser user, HttpSession session) {
        if (user == null) {
            return LgResult.build("404", "数据错误");
        }
        if (user.getLoginname() == null) {
            return LgResult.build("404", "用户名称不存在");
        }
        if (user.getLoginpass() == null) {
            return LgResult.build("404", "用户密码不存在");
        }
        System.out.println(user);
        // 查询用户是否存在
        TbUser oldUser = userService.findByCondition(user);
        if (oldUser != null && oldUser.getId() != null) {
            //将用户对象添加到Session中
            session.setAttribute("USER_SESSION", user);
            return LgResult.build();
        }
        return LgResult.build("301", "改用户不存在");
    }

}
